package com.example.mydiary;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DiaryListActivity extends AppCompatActivity {
    MyDiaryDatabase mdb;
    ListView listdb;
    Button b1;
    EditText et;
    private List  data = new ArrayList();
    private int nid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diary_list);
        listdb = (ListView) findViewById(R.id.list);
        b1=(Button)findViewById(R.id.button);
        et=(EditText)findViewById(R.id.editText);
        b1.setOnClickListener(new mclick());
        mdb = new MyDiaryDatabase(this);
        mdb.opendb();
        mdb.onCreateTable();
        init2(0);
        listdb.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                HashMap map = (HashMap)data.get(position);
                nid = (int) map.get("id");
                Intent intent=new Intent(DiaryListActivity.this,DiaryDetail.class);
                Bundle bb = new Bundle();
                bb.putInt("ID",nid);
                bb.putInt("flag",1);//flag 修改是1，添加是0
                intent.putExtras(bb);
                startActivityForResult(intent,0);
            }
        });
    }

    private void init2(int a) {
        Cursor cursor;
        listdb.clearAnimation();
        if(a==1){//a为1是搜索关键字
            String s=et.getText().toString();//获得用户输入的关键字
            cursor= mdb.sousuo(s);
        }else{
            cursor = mdb.getAllNotes();
        }
        data.clear();
        if (cursor != null) {
            int num = cursor.getCount();
            if(num == 0) {
                if(a==0)
                    Toast.makeText(this, "没有数据,选菜单添加数据", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(this,"搜索结果为空", Toast.LENGTH_LONG).show();
                return;
            }
            //Toast.makeText(this, Integer.toString(num), Toast.LENGTH_LONG).show();
            while (cursor.moveToNext()) {
                int id = cursor.getInt(0);
                String body = cursor.getString(1);
                String time=cursor.getString(2);

                Map map = new HashMap();
                if(body.length()>15)body=body.substring(0,15);
                map.put("id", id);
                map.put("body", body);
                map.put("time", time);
                data.add(map);
            }
            cursor.close();
            SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,
                    android.R.layout.simple_expandable_list_item_1,
                    new String[]{ "time","body"},
                    new int[]{android.R.id.text1,android.R.id.text2});
            SimpleAdapter simpleAdapter2 = new SimpleAdapter(this, data,
                    R.layout.diary_row,
                    new String[]{ "body","time"},
                    new int[]{R.id.text1,R.id.text2});
            listdb.setAdapter(simpleAdapter2);
        }
    }
    class mclick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            if(v==b1){
                init2(1);
            }
        }
    }
    private static final int INSERT_ID = Menu.FIRST;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.menu_insert);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case INSERT_ID:
                Intent intent=new Intent(DiaryListActivity.this,DiaryDetail.class);
                Bundle bb = new Bundle();
                bb.putInt("flag",0);//flag 修改是1，添加是0
                intent.putExtras(bb);
                startActivityForResult(intent,0);
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }
}